package com.demo.ipcSocket;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

/**
 * @author MT
 * 客户端心跳连接
 * @date 2019-12-09 13:18
 */
@Slf4j
@Data
public class ClientSocket implements Runnable {
    private Socket socket;
    private DataInputStream inputStream;
    private DataOutputStream outputStream;
    private String key;
    private String message;
    private boolean isCloseStream = false;

    public void shutdownStream(){
        try {
            if (!isCloseStream() && socket != null) {
                socket.shutdownInput();
                isCloseStream = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
            isCloseStream = false;
        }
    }

    @Override
    public void run() {
        //每5秒进行一次客户端连接，判断是否需要释放资源
        while (true){
            try {
                TimeUnit.SECONDS.sleep(5);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (SocketHandler.isSocketClosed(this)){
                break;
            }
        }
    }
}
